package com.mall.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.dto.PmsProductDto;
import com.mall.pageResult.PageResult;
import com.mall.pojo.PmsProduct;
import com.mall.service.PmsProductService;
import com.mall.utils.Result;
import com.mall.vo.PmsProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/product")
public class PmsProductController {

    @Autowired
    private PmsProductService pmsProductService;

    //创建商品
    @PostMapping("/create")
    public Result create(@RequestBody PmsProductDto pmsProductDto) {
        pmsProductService.create(pmsProductDto);
        return Result.success(null, "操作成功");
    }

    //查询商品
    @GetMapping("/list")
    public Result<PageResult<PmsProduct>> list(@RequestParam(value = "brandId", required = false) Integer brandId,
                                               @RequestParam(value = "keyword", required = false) String keyword,  //模糊查询
                                               @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                               @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                               @RequestParam(value = "productCategoryId", required = false) Integer productCategoryId,
                                               @RequestParam(value = "productSn", required = false) String productSn,
                                               @RequestParam(value = "publishStatus", required = false) Integer publishStatus,
                                               @RequestParam(value = "verifyStatus", required = false) Integer verifyStatus) {
        Page<PmsProduct> page = pmsProductService.list(brandId, keyword, pageNum, pageSize, productCategoryId, productSn, publishStatus, verifyStatus);
        PageResult<PmsProduct> pageResult = new PageResult<>();
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);
        if (CollUtil.isEmpty(page.getRecords())) {
            pageResult.setList(new ArrayList<>());
            return Result.success(pageResult, "操作成功");
        }
        pageResult.setTotal(Convert.toInt(page.getTotal()));
        pageResult.setTotalPage(Convert.toInt(page.getPages()));
        pageResult.setList(page.getRecords());
        return Result.success(pageResult, "操作成功");
    }

    //根据商品名称或货号模糊查询
    @GetMapping("/simpleList")
    public Result<List<PmsProduct>> simpleList(@RequestParam("keyword") String keyword) {
        List<PmsProduct> list = pmsProductService.simpleList(keyword);
        return Result.success(list, "操作成功");
    }

    //批量修改删除状态
    @PostMapping("/update/deleteStatus")
    public Result<PmsProduct> deleteStatus(@RequestParam("deleteStatus") Integer deleteStatus,
                                           @RequestParam("ids") List<Integer> ids) {
        pmsProductService.deleteStatus(ids, deleteStatus);
        return Result.success(null, "操作成功");
    }

    //批量设为新品
    @PostMapping("/update/newStatus")
    public Result<PmsProduct> newStatus(@RequestParam("newStatus") Integer newStatus,
                                        @RequestParam("ids") List<Integer> ids) {
        pmsProductService.newStatus(ids, newStatus);
        return Result.success(null, "操作成功");
    }

    //批量上下架商品
    @PostMapping("/update/publishStatus")
    public Result<PmsProduct> publishStatus(@RequestParam("publishStatus") Integer publishStatus,
                                            @RequestParam("ids") List<Integer> ids) {
        pmsProductService.publishStatus(ids, publishStatus);
        return Result.success(null, "操作成功");
    }

    //批量推荐商品
    @PostMapping("/update/recommendStatus")
    public Result<PmsProduct> recommendStatus(@RequestParam("recommendStatus") Integer recommendStatus,
                                              @RequestParam("ids") List<Integer> ids) {
        pmsProductService.recommendStatus(ids, recommendStatus);
        return Result.success(null, "操作成功");
    }

    //批量修改审核状态(detail未使用到?)
    @PostMapping("/update/verifyStatus")
    public Result<PmsProduct> verifyStatus(@RequestParam("verifyStatus") Integer verifyStatus,
                                           @RequestParam("detail") String detail,
                                           @RequestParam("ids") List<Integer> ids) {
        pmsProductService.verifyStatus(ids, detail, verifyStatus);
        return Result.success(null, "操作成功");
    }

    //更新商品
    @PostMapping("/update/{id}")
    public Result update(@PathVariable("id") Integer id,
                         @RequestBody PmsProductDto pmsProductDto) {
        pmsProductService.update(id, pmsProductDto);
        return Result.success(null, "操作成功");
    }

    //根据商品id获取商品编辑信息
    @GetMapping("/updateInfo/{id}")
    public Result<PmsProductVo> updateInfo(@PathVariable("id") Integer id) {
      PmsProductVo pmsProductVo=  pmsProductService.updateInfo(id);
        return Result.success(pmsProductVo, "操作成功");
    }

}
